﻿Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Web.Services.Protocols
Imports VSModel
Imports System.Data.Objects
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class UpdateDataCertifiedHouseHold
    Inherits System.Web.Services.WebService

    <WebMethod()> _
       <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetData(ByVal _huyenID As Integer) As List(Of CertifiedHouseHoldData)
        Dim _lst As New List(Of CertifiedHouseHoldData)
        Try
            Using data As New VSEntities
                'Load du lieu ung voi ky huyenID
                Dim p = (From m In data.CertifiedHouseholds Where m.ProvinceId = _huyenID And If(m.FlagUpdate, 0) = 0).ToList

                'Export du lieu sang kieu du lieu json
                If Not p Is Nothing AndAlso p.Count > 0 Then
                    For i As Integer = 0 To p.Count - 1
                        _lst.Add(New CertifiedHouseHoldData() With { _
                                 .CertifiedID = p(i).CertifiedID,
                                 .HuyenID = _huyenID,
                                 .CertifiedCode = p(i).CertifiedCode,
                                 .CertifiedDate = p(i).CertifiedDate,
                                 .Biz_VietNam = p(i).Biz_VietName,
                                 .Biz_HeadOffice = p(i).Biz_HeadOffice,
                                 .Biz_Tel = p(i).Biz_Tel,
                                 .Biz_Fax = p(i).Biz_Fax,
                                 .Biz_Email = p(i).Biz_Email,
                                 .Biz_Website = p(i).Biz_Website,
                                 .NganhNgheKD = p(i).NganhNgheKinhDoanh,
                                 .Biz_InvestCapital = p(i).Biz_InvestCapital,
                                 .Biz_WordsMoney = p(i).Biz_WordsMoney,
                                 .OW_Name = p(i).Ow_Name,
                                 .OW_Sex = p(i).Ow_Sex,
                                 .Ow_BirthDate = p(i).Ow_BirthDate,
                                 .Ow_National_ID = p(i).Ow_National_ID,
                                 .Ow_Nationality_ID = p(i).Ow_Nationality_ID,
                                 .Ow_CardID = p(i).Ow_CardID,
                                 .Ow_CardDate = p(i).Ow_CardDate,
                                 .Ow_Provider_ID = p(i).Ow_Provider_ID,
                                 .Ow_Resident = p(i).Ow_Resident,
                                 .Ow_Present = p(i).Ow_Present})

                        ''Cập nhật lai FlagUpdate cho những CertifiedHousehold
                        'p(i).FlagUpdate = 1
                    Next
                    ''Save Data change
                    'data.SaveChanges()
                End If
            End Using
        Catch ex As Exception
        End Try
        Return _lst

    End Function

    <WebMethod()> _
    Public Function UpdateData(ByVal lstID As String) As Integer
        Try
            lstID = If(lstID.LastIndexOf(",") = -1, lstID, lstID.Substring(0, lstID.LastIndexOf(",")))
            Dim _sql As String = "Update CertifiedHouseHold set FlagUpdate =1 WHERE CertifiedID in (" & lstID & ")"
            Using data As New VSEntities
                data.ExecuteStoreCommand(_sql)
                Return 1
            End Using
        Catch ex As Exception
            Return 0
        End Try
    End Function

End Class